package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.act.ActDay;
import com.motu.monstercity.server.game.userdata.UserAct;
import com.motu.monstercity.server.game.userdata.UserActDayTask;
import com.motu.monstercity.server.game.userdata.UserActTaskReward;
import com.motu.monstercity.server.game.userdata.UserExtInfo;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;

import java.util.ArrayList;
import java.util.List;

public class ActDaysUserData extends ActDaysBaseData {

    public static UserAct getUserActDays(long userId, long actId) {
        return (UserAct) EntityManager.getEntity(UserAct.class, userId, actId);
    }


    public static UserActDayTask getUserActDayTask(long userId, long actId, int taskId) {
        return (UserActDayTask) EntityManager.getEntity(UserActDayTask.class, EntityManager.getFieldJoinString(userId, actId), taskId);
    }

    public static List<UserActDayTask> getUserActDayTasks(long userId, long actId) {
        List<Model> modelList = EntityManager.getEntityList(UserActDayTask.class, EntityManager.getFieldJoinString(userId, actId));
        List<UserActDayTask> userActDayTasks = new ArrayList<>();
        for (Model model : modelList) {
            userActDayTasks.add((UserActDayTask) model);
        }
        return userActDayTasks;
    }

    /**
     * 获取用户主题活动数据
     */
    public static UserAct getUserMotifAct(long userId, long actId) {
        UserAct userMotifAct = (UserAct) EntityManager.getEntity(UserAct.class, userId, actId);
        if (userMotifAct != null) {
            return userMotifAct != null && userMotifAct.isOpen() ? userMotifAct : null;
        }
        if (actId < UserAct.NEWCOMER_ACT_ID_START) {
            // 限时主题活动
        } else {
            UserExtInfo userExtInfo = GameUser.getUserExtInfo(userId);
            int themeGroup = ActManager.getNewcomerMotifGroup(userExtInfo);
            if (themeGroup != 0) {
                // 原先的新手活动，增加到分组4，不会再增加了
                ActDay actTheme = getActDaysByDay(themeGroup, 1);
                if (actTheme != null) {
                    long[] newStartActTimeInfo = ActManager.getNewStartActTimeInfo(userExtInfo);
                    actId = actTheme.getGroupId() + UserAct.NEWCOMER_ACT_ID_START;
                    if (userMotifAct == null) {
                        userMotifAct = new UserAct(userId, actId, actTheme.getGroupId(),
                                newStartActTimeInfo[0], newStartActTimeInfo[1]);
                        userMotifAct.doCreate();
                    }
                }
            }
        }
        return userMotifAct != null && userMotifAct.isOpen() ? userMotifAct : null;
    }

    public static long getNewComerActId(UserExtInfo userExtInfo) {
        int themeGroup = ActManager.getNewcomerMotifGroup(userExtInfo);
        if (themeGroup != 0) {
            ActDay actTheme = getActDaysByDay(themeGroup, 1);
            if (actTheme != null) {
                return actTheme.getGroupId() + UserAct.NEWCOMER_ACT_ID_START;
            }
        }
        return 0;
    }

}
